/*
   Copyright 2011 Brook Smith

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package org.xempot.xform;

import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

public class InstructionData {
	private static final XPathFactory X_PATH_FACTORY = XPathFactory.newInstance();

	private final String xpath;

	private final String command;
	private final String template;
	private final String altTemplate;
	private XPathExpression xpathExprs;

	private final Instruction instruction;

	public InstructionData(Instruction instruction, String xpath, String command, String template, String altTemplate)
			throws InstructionException {
		super();
		this.instruction = instruction;
		this.xpath = xpath;
		this.command = command;
		this.template = template;
		this.altTemplate = altTemplate;

		if (xpath != null) {
			try {
				xpathExprs = X_PATH_FACTORY.newXPath().compile(xpath);
			} catch (XPathExpressionException e) {
				throw new InstructionException(xpath, e);
			}
		}
	}

	public String getAltTemplate() {
		return altTemplate;
	}

	public String getCommand() {
		return command;
	}

	public Instruction getInstruction() {
		return instruction;
	}

	public String getTemplate() {
		return template;
	}

	public String getXpath() {
		return xpath;
	}

	public XPathExpression getXpathExprs() {
		return xpathExprs;
	}

	@Override
	public String toString() {
		return "TemplateData [xpath=" + xpath + ", command=" + command + ", template=" + template + ", altTemplate="
				+ altTemplate + ", type=" + instruction + "]";
	}
}